#!/bin/sh

# $OpenBSD: arduinoproject,v 1.3 2014/07/18 11:12:13 edd Exp $
# Copyright (c) 2011 Chris Kuethe <ckuethe@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

L=${TRUEPREFIX}
AS="${L}/share"
ASA="${AS}/arduino"
ASD="${AS}/doc/arduino"
ASE="${AS}/examples/arduino"

if [ $# -ne 1 -o "x$1" = "x-h" ] ; then
	cat << EOH
usage: $0 <projectname>

This will populate a new directory with a makefile and a template
source file. If you have an existing source - one of the examples,
perhaps - you can copy it over the template and run "make".


EOH
	exit 1
fi

P=$(echo -n "$1" | tr -c -s "A-Za-z0-9_-." "_")

if [ -d "$P" ] ; then
	echo "using existing $P directory"
else
	if [ -e "$P" ] ; then
		echo "sorry, $P exists and is not a directory"
		exit 1
	fi
	T=`mktemp -d arduino.XXXXXXXXXX` || exit 1
	echo "creating new arduino project '$P'"
	mv $T "$P"
fi
cd $P || exit 1

D=`date "+.%s"`
cmp -s ${ASE}/Makefile.in Makefile || \
	install -b -B $D -S ${ASE}/Makefile Makefile
cmp -s ${ASE}/template.ino "${P}.ino" || \
	install -b -B $D -S ${ASE}/template.ino "${P}.ino"
mkdir -p applet
